import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/routes/routePages.dart';
import 'package:ichinesereader/themes/pageStyles.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/charts/booklevel.dart';
import 'package:ichinesereader/widgets/charts/booktopics.dart';
import 'package:ichinesereader/widgets/grids/bookSLiderGridNoBar.dart';
import 'package:ichinesereader/widgets/layouts/appdrawerSchoolAdmin.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
import 'package:ichinesereader/widgets/sidebars/adminSidebar.dart';
import '../../themes/appTheme.dart';

class SchoolAdminBookOverview extends StatefulWidget {
  SchoolAdminBookOverview({Key? key}) : super(key: key);
  static const String routeName = '/schooladmin/bookoverview';

  @override
  State<SchoolAdminBookOverview> createState() =>
      _SchoolAdminBookOverviewState();
}

class _SchoolAdminBookOverviewState extends State<SchoolAdminBookOverview> {
  final box = Hive.box('appconfig');
  final BooksController _booksController = Get.find();

  @override
  void initState() {
    bool isSchoolAdmin = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_SchoolAdmin;

    if (isSchoolAdmin) {
      Future.delayed(Duration(milliseconds: 100), () {
        if (_booksController.bookleveldata.isEmpty) {
          _booksController.getBooksDataSummary();
        }
        if (_booksController.mostReadBookItems.isEmpty) {
          _booksController.fetchMostReadBooksItems();
          _booksController.fetchMostReadBooksItemsSchool();
        }
      });
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build SchoolAdmin BookOverview');

    final isSmallScreen =
        MediaQuery.of(context).size.width < Constants.DefinedScreenSmallSize;
    final isBigScreen =
        MediaQuery.of(context).size.width > Constants.DefinedScreenBigSize;
    int _selectedSidebarItem = RoutePages.schoolAdminPageItems
        .indexWhere((page) => page.route == Get.currentRoute);
    final PageStyles _pageStyles = PageStyles();

    return Scaffold(
      appBar: isSmallScreen ? CustomAppBar(title: 'Book Overview'.tr) : null,
      drawer: isSmallScreen ? SchoolAdminAppDrawer() : null,
      body: Container(
        decoration: _pageStyles.pageBoxDecoration(),
        child: Row(
          children: [
            Visibility(
                visible: !isSmallScreen,
                child: AdminSidebarX(
                    selectedIndex: _selectedSidebarItem,
                    isBigScreen: isBigScreen)),
            Expanded(
              child: Container(
                margin: _pageStyles.mainContainerPageEdgeInsets(),
                child: SingleChildScrollView(
                  scrollDirection: Axis.vertical,
                  //padding: _pageStyles.pageEdgeInsets(),
                  child: Column(
                    children: [
                      Container(
                        child: isSmallScreen
                            ? Column(
                                children: [
                                  Container(
                                    margin: EdgeInsets.only(bottom: 4.5),
                                    decoration: BoxDecoration(
                                      color: Get.isDarkMode
                                          ? AppTheme.colors.darkTheme2
                                          : Colors.white.withOpacity(0.1),
                                      borderRadius: BorderRadius.circular(6.0),
                                    ),
                                    child: Obx(() {
                                      if (_booksController
                                          .booktopicsdata.isNotEmpty)
                                        return Container(
                                          decoration: BoxDecoration(
                                            borderRadius:
                                                BorderRadius.circular(6.0),
                                          ),
                                          child: BookTopicsWidget(
                                              bookchartdata: _booksController
                                                  .booktopicsdata),
                                        );
                                      else
                                        return Text('');
                                    }),
                                  ),
                                  Container(
                                    margin: EdgeInsets.only(top: 4.5),
                                    decoration: BoxDecoration(
                                      color: Get.isDarkMode
                                          ? AppTheme.colors.darkTheme2
                                          : Colors.white.withOpacity(0.1),
                                      borderRadius: BorderRadius.circular(6.0),
                                    ),
                                    child: Obx(() {
                                      if (_booksController
                                          .bookleveldata.isNotEmpty)
                                        return Container(
                                          decoration: BoxDecoration(
                                            borderRadius:
                                                BorderRadius.circular(6.0),
                                          ),
                                          child: BookLevelWidget(
                                              bookchartdata: _booksController
                                                  .bookleveldata),
                                        );
                                      else
                                        return Text('');
                                    }),
                                  ),
                                ],
                              )
                            : Row(
                                children: [
                                  Expanded(
                                    child: Container(
                                      margin: EdgeInsets.only(right: 4.5),
                                      decoration: BoxDecoration(
                                        color: Get.isDarkMode
                                            ? AppTheme.colors.darkTheme2
                                            : Colors.white.withOpacity(0.1),
                                        borderRadius:
                                            BorderRadius.circular(6.0),
                                      ),
                                      child: Obx(() {
                                        if (_booksController
                                            .booktopicsdata.isNotEmpty)
                                          return Container(
                                            decoration: BoxDecoration(
                                              borderRadius:
                                                  BorderRadius.circular(6.0),
                                            ),
                                            child: BookTopicsWidget(
                                                bookchartdata: _booksController
                                                    .booktopicsdata),
                                          );
                                        else
                                          return Text('');
                                      }),
                                    ),
                                  ),
                                  Expanded(
                                    child: Container(
                                      margin: EdgeInsets.only(left: 4.5),
                                      decoration: BoxDecoration(
                                        color: Get.isDarkMode
                                            ? AppTheme.colors.darkTheme2
                                            : Colors.white.withOpacity(0.1),
                                        borderRadius:
                                            BorderRadius.circular(6.0),
                                      ),
                                      child: Obx(() {
                                        if (_booksController
                                            .bookleveldata.isNotEmpty)
                                          return Container(
                                            decoration: BoxDecoration(
                                              borderRadius:
                                                  BorderRadius.circular(6.0),
                                            ),
                                            child: BookLevelWidget(
                                                bookchartdata: _booksController
                                                    .bookleveldata),
                                          );
                                        else
                                          return Text('');
                                      }),
                                    ),
                                  )
                                ],
                              ),
                      ),
                      Container(
                        margin: EdgeInsets.only(top: 9.0),
                        decoration: BoxDecoration(
                          color: Get.isDarkMode
                              ? AppTheme.colors.darkTheme2
                              : Colors.white.withOpacity(0.1),
                          borderRadius: BorderRadius.circular(3.0),
                        ),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Container(
                              margin: EdgeInsets.only(left: 15.0, top: 15.0),
                              child: Text(
                                'Most Popular Books at iChineseReader'.tr,
                                style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 21.0,
                                    fontFamily: 'Montserrat'),
                              ),
                            ),
                            Container(
                              margin: EdgeInsets.all(18.0),
                              child: Obx(() {
                                if (_booksController
                                    .mostReadBookItemsSchool.isNotEmpty)
                                  return Container(
                                    height: 270,
                                    width: double.infinity,
                                    decoration: BoxDecoration(),
                                    child: BookSliderGridNoBar(
                                        books: _booksController
                                            .mostReadBookItemsSchool),
                                  );
                                else
                                  return Text('');
                              }),
                            ),
                          ],
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.only(top: 9.0),
                        decoration: BoxDecoration(
                          color: Get.isDarkMode
                              ? AppTheme.colors.darkTheme2
                              : Colors.white.withOpacity(0.1),
                          borderRadius: BorderRadius.circular(3.0),
                        ),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Container(
                              margin: EdgeInsets.only(left: 15.0, top: 15.0),
                              child: Text(
                                'Most Popular Books in Your School'.tr,
                                style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 21.0,
                                    fontFamily: 'Montserrat'),
                              ),
                            ),
                            Container(
                              margin: EdgeInsets.all(18.0),
                              child: Obx(() {
                                if (_booksController
                                    .mostReadBookItems.isNotEmpty)
                                  return Container(
                                    height: 270,
                                    width: double.infinity,
                                    child: BookSliderGridNoBar(
                                        books:
                                            _booksController.mostReadBookItems),
                                  );
                                else
                                  return Text('');
                              }),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
